﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using KCMS.BusinessLogic.Interfaces;
using KCMS.DataAcsess.Interfaces;
using StructureMap;
using AutoMapper;
using KCMS.BusinessLogic.Models;

namespace KCMS.BusinessLogic.Engine
{
    public class ProductCategoryService : IProductCategoryService
    {
        private IProductCategoryManager pgm = ObjectFactory.GetInstance<IProductCategoryManager>();
        //private IPictureService pictureSevice = ObjectFactory.GetInstance<IPictureService>();
        public Models.ProductCategory getById(Guid id)
        {
            DataAcsess.Entitys.ProductCategoryEntity productCategoryEntity = pgm.getById(id);
            return Mapper.Map<DataAcsess.Entitys.ProductCategoryEntity, Models.ProductCategory>(productCategoryEntity);
        }
        public Models.ProductCategory getByCode(String code)
        {


            var productCategoryEntity = (from t in pgm.getAll()
                                         where (t.Code.Equals(code))
                                         select t).SingleOrDefault();
            return Mapper.Map<DataAcsess.Entitys.ProductCategoryEntity, Models.ProductCategory>(productCategoryEntity);
        }

        public IEnumerable<Models.ProductCategory> getAll()
        {
            return Mapper.Map<IEnumerable<DataAcsess.Entitys.ProductCategoryEntity>, IEnumerable<Models.ProductCategory>>(pgm.getAll());

        }

        public void update(ProductCategory productCategory)
        {
            pgm.update(Mapper.Map<ProductCategory, DataAcsess.Entitys.ProductCategoryEntity>(productCategory));
        }


        public void save(ProductCategory productCategory)
        {
            pgm.save(Mapper.Map<ProductCategory, DataAcsess.Entitys.ProductCategoryEntity>(productCategory));
        }


        public void delete(Guid id)
        {
            pgm.delete(id);
        }


        public IEnumerable<ProductCategory> getAllParents(ProductCategory category)
        {
            List<ProductCategory> parents = new List<ProductCategory>();
            while (category.ParentId != Guid.Empty)
            {
                category = getById(category.ParentId);
                parents.Add(category);
            }
            parents.Reverse();
            return parents;
        }

    }
}
